package com.nuaa.ange.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.nuaa.ange.common.lang.RespBean;
import com.nuaa.ange.dto.ArticleDto;
import com.nuaa.ange.entity.Article;
import com.nuaa.ange.entity.ArticlePic;
import com.nuaa.ange.entity.User;
import com.nuaa.ange.mapper.ArticleMapper;
import com.nuaa.ange.service.ArticlePicService;
import com.nuaa.ange.service.ArticleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nuaa.ange.service.UserService;
import com.sun.xml.internal.bind.v2.TODO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @description
 * @author 玦离
 * @date 2022/12/30 20:56
 */

@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {

    @Autowired
    UserService userService;

    @Autowired
    ArticlePicService articlePicService;

    @Override
    public int saveArticle(Article article, int userId, int type) {
        User author = userService.getById(userId);
        // 保存为草稿状态
        article.setArticleState(type);
        article.setAuthorId(userId);
        // 无论是否禁言只需要负责插入保存
        this.save(article);
        return article.getArticleId();
    }

    @Override
    public Article getDraftByAuthorId(int authorId) {
        return this.getOne(new QueryWrapper<Article>()
                .eq("author_id", authorId)
                .eq("article_state", 0));
    }

    @Override
    public ArticleDto getArticleDtoById(int userId, int articleId){
        User author = userService.getById(userId);
        boolean userRole = author.getUserRole();
        Article article = this.getById(articleId);
        ArticleDto articleDto = new ArticleDto();
        articleDto.setArticleId(articleId);
        articleDto.setTitle(article.getTitle());
        articleDto.setContent(article.getContent());
        articleDto.setReplies(article.getReplies());
        articleDto.setTags(article.getTags());
        articleDto.setAuthorId(article.getAuthorId());
        articleDto.setUpdateTime(article.getUpdateTime());
        articleDto.setStars(article.getStars());
        articleDto.setArticleState(article.getArticleState());
        List<ArticlePic> allArticlePic = articlePicService.list(new QueryWrapper<ArticlePic>()
                .eq("article_id", articleId));
        // TODO: 2023/1/3 是否按照时间顺序
//        articleDto.setPicUrl(allArticlePic);
        articleDto.setReportedNum(article.getReportedNum());
        articleDto.setNickname(author.getNickname());
        articleDto.setHeadPortrait(author.getHeadPortrait());
//        Scanhistory scanhistory = scanhistoryMapper.getMyScanHitroyId(userId, articleId);
//        if (scanhistory != null && scanhistory.getScanUserState() == userAR && scanhistory.getStarState() == 1) {
//            articleUser.setStarState(1);
//        } else {
//            articleUser.setStarState(0);
//        }
        return articleDto;
    }


}
